package org.mparts.client.activity;

import java.util.List;

import org.mparts.client.place.CatalogPlace;
import org.mparts.client.ui.IMenuView;
import org.mparts.client.ui.ILoadCatalogView.ILoadCatalogPresenter;
import org.mparts.client.ui.IMenuView.IMenuPresenter;
import org.mparts.client.ui.events.LoadCatalogEvent;
import org.mparts.client.ui.events.LoadUserEvent;
import org.mparts.client.ui.events.LoadUserEventHandler;
import org.mparts.client.utils.Utils;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

public class MenuActivity extends AbstractMpartsActivity implements IMenuPresenter {

	private final EventBus eventBus;
	private final IMenuView display;
	private final PlaceController placeController;
	
	private Provider<CatalogPlace> catalogPlaceProvider;
	private ILoadCatalogPresenter loadCatalogactivity;

    @Inject
	public MenuActivity(EventBus eventBus, IMenuView display,
			PlaceController placeController,
			Provider<CatalogPlace> catalogPlaceProvider,
			ILoadCatalogPresenter loadCatalogactivity
			) {
		this.eventBus = eventBus;
		this.display = display;
		this.placeController = placeController;
		this.catalogPlaceProvider = catalogPlaceProvider;
		this.loadCatalogactivity = loadCatalogactivity;
		
		bind();
	}
    
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		display.setPresenter(this);
	}
	
	private void bind() {
//		display.getSearchButton().addSelectHandler(new SelectHandler() {
//			
//			@Override
//			public void onSelect(SelectEvent event) {
//				if (display.getSearchField().getValue().length() > 0) {
//					Info.display("Search", "Search '" + display.getSearchField().getValue() + "' started...");
//					//placeController.goTo(newPlace);
//				}
//				
//			}
//		});
//		
//		display.getCatalogButton().addSelectHandler(new SelectHandler() {
//			
//			@Override
//			public void onSelect(SelectEvent event) {
//				CatalogPlace place = catalogPlaceProvider.get();
//				goTo(place);
//			}
//		});
		
		Log.debug("add handler to login button");
		display.getLoginButton().addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				Log.debug("login pressed...");
				Utils.getMpartsService().getUser(Window.Location.getHref(), new AsyncCallback<List<String>>() {

					@Override
					public void onFailure(Throwable caught) {
						Log.error("load user data failed");
						
					}

					@Override
					public void onSuccess(List<String> result) {
						// TODO Auto-generated method stub
						Utils.setUserData(result);
						List<String> userData = Utils.getUserData();
						if (userData.get(0).length() > 0) {
							// display.getLoginButton().setText("Logout");
							final String logoutURL = userData.get(2);
							Log.info("redirect to: " + logoutURL);
							Window.Location.assign(logoutURL);
						} else {
							// display.getLoginButton().setText("Login");
							final String loginURL = userData.get(1);
							Log.info("redirect to: " + loginURL);
							Window.Location.assign(loginURL);
						}
					}
					
				});
			}
		});
		
		eventBus.addHandler(LoadUserEvent.TYPE, new LoadUserEventHandler() {
			
			@Override
			public void onLoad(LoadUserEvent saveEvent) {
				Log.debug("load user event handled");
				List<String> userData = Utils.getUserData();
				if (userData.get(0).length() > 0) {
					display.getLoginButton().setText("Logout");
					display.setUserName(userData.get(0));
				} else {
					display.getLoginButton().setText("Login");
					display.setUserName("");
				}
				display.getLoadCatalogButton().setVisible(Utils.isManager());
			}
		});
		
		Log.debug("add load catalog handler");
		display.getLoadCatalogButton().addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				Log.debug("fire load catalog event");
				eventBus.fireEvent(new LoadCatalogEvent());
			}
		});

	}

	@Override
	public void goTo(Place place) {
		placeController.goTo(place);		
	}

	@Override
	public IMenuView getView() {
		return display;
	}

}
